#include "mycleanusbdialog.h"
#include "ui_mycleanusbdialog.h"

myCleanUSBDialog::myCleanUSBDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::myCleanUSBDialog)
{
    ui->setupUi(this);
}

myCleanUSBDialog::~myCleanUSBDialog()
{
    delete ui;
}

void myCleanUSBDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

bool myCleanUSBDialog::run_form()
{
    fillWithDrives();

    return exec();
}

void myCleanUSBDialog::fillWithDrives()
{
    char buf[26];
    GetLogicalDriveStringsA(sizeof(buf),buf);

    for( char *s = buf; *s; s+=strlen(s)+1 )
    {
        if ( GetDriveTypeA(s) == 2 )
        {
            QString driveName;
            QString driveSerial;
            QString driveFreeSize;
            unsigned  __int64 freeBytes;
            unsigned  __int64 totalBytes;
            unsigned  __int64 allFree;

            QString letter = QString(s);
            LPCTSTR szHD = (LPCTSTR)letter.utf16();
            UCHAR szFileSys[255],szVolNameBuff[255];
            DWORD dwSerial,dwMFL,dwSysFlags;

            if ( GetVolumeInformation(szHD,(LPTSTR)szVolNameBuff,
                    255,&dwSerial, &dwMFL,&dwSysFlags,
                    (LPTSTR)szFileSys,255) )
            {
                driveName = QString::fromUtf16((ushort*)szVolNameBuff);
                driveSerial = QString::number(dwSerial);
            }

            if ( GetDiskFreeSpaceEx( szHD,
                                (PULARGE_INTEGER)&freeBytes,
                                (PULARGE_INTEGER)&totalBytes,
                                (PULARGE_INTEGER)&allFree ) )
            {
               driveFreeSize = QString::number(allFree, 10);
            }

            if ( driveName.length() > 0 )
            {
                QTreeWidgetItem * item = new QTreeWidgetItem( ui->treeWidget, 0 );
                connect( ui->treeWidget, SIGNAL(itemDoubleClicked ( QTreeWidgetItem *, int )),
                         this, SLOT( itemDoubleClicked( QTreeWidgetItem *, int) ) );
                item->setCheckState( 0, Qt::Checked );
                item->setText( 0, letter );
                item->setText( 1, driveName );
                item->setText( 2, driveSerial );
                item->setText( 3, driveFreeSize );
            }
        }
    }
    for ( int i = 0; i < ui->treeWidget->columnCount(); i++ )
        ui->treeWidget->resizeColumnToContents(i);
}

drivesDataList myCleanUSBDialog::getAllDataList()
{
    fillWithDrives();

    genDriveList.clear();
    QTreeWidgetItemIterator it( ui->treeWidget, QTreeWidgetItemIterator::Checked );
    while (*it)
    {
        drivesData myDriveData;
        myDriveData.driveLetter = (*it)->text(0);
        myDriveData.driveLabel  = (*it)->text(1);
        myDriveData.driveSerial = (*it)->text(2);
        bool ok;
        if ( (*it)->text(3).toLongLong(&ok, 10) <= 0 )
            myDriveData.driveFreeSize = 0;
        else
            myDriveData.driveFreeSize = (*it)->text(3).toLongLong(&ok, 10);
        genDriveList.append( myDriveData );
        ++it;
    }

    return genDriveList;
}

void myCleanUSBDialog::itemDoubleClicked ( QTreeWidgetItem * item, int )
{
    if ( item->checkState(0) == Qt::Unchecked )
        item->setCheckState( 0, Qt::Checked );
    else
        item->setCheckState( 0, Qt::Unchecked );
}

void myCleanUSBDialog::on_selectLabel_linkActivated(QString link)
{
    if ( ui->treeWidget->topLevelItemCount() > 0 )
    {
        if ( link == "Select all")
        {
            ui->selectLabel->setText("<a href=\"Deselect all\">Deselect all</A>");

            QTreeWidgetItemIterator it( ui->treeWidget, QTreeWidgetItemIterator::NotChecked );
            while (*it)
            {
                (*it)->setCheckState(0, Qt::Checked );
                ++it;
            }
            ui->cleanupButton->setEnabled( true );
        }
        else
        {
            ui->selectLabel->setText("<a href=\"Select all\">Select all</A>");

            QTreeWidgetItemIterator it( ui->treeWidget, QTreeWidgetItemIterator::Checked );
            while (*it)
            {
                (*it)->setCheckState(0, Qt::Unchecked );
                ++it;
            }
            ui->cleanupButton->setEnabled( false );
        }
    }
}

void myCleanUSBDialog::on_rescanLabel_linkActivated(QString)
{
#ifndef QT_NO_CURSOR
    setCursor(Qt::WaitCursor);
#endif
    ui->treeWidget->clear();
    fillWithDrives();
#ifndef QT_NO_CURSOR
        setCursor(Qt::ArrowCursor);
#endif
}

void myCleanUSBDialog::on_cleanupButton_clicked()
{
    genDriveList.clear();
    QTreeWidgetItemIterator it( ui->treeWidget, QTreeWidgetItemIterator::Checked );
    while (*it)
    {
        drivesData myDriveData;
        myDriveData.driveLetter = (*it)->text(0);
        myDriveData.driveLabel  = (*it)->text(1);
        myDriveData.driveSerial = (*it)->text(2);
        bool ok;
        if ( (*it)->text(3).toLongLong(&ok, 10) <= 0 )
            myDriveData.driveFreeSize = 0;
        else
            myDriveData.driveFreeSize = (*it)->text(3).toLongLong(&ok, 10);
        genDriveList.append( myDriveData );
        ++it;
    }

    accept();
}
